<script setup lang="ts">
import {ElMessage} from "element-plus";
import {useCartStore} from "@/stores/cartStore";
import {computed, onMounted} from "vue";
import {useOrderStore} from "@/stores/orderStore";
import {alipay, updatePayTime} from "@/router/api";
import {authUtilStore} from "@/utils/authStore";
import {useUserStore} from "@/stores/userStore";
import {useRouter} from "vue-router";

const cartStore = useCartStore()
const router = useRouter()
const userInfo = useUserStore().userInfo
const orderStore = useOrderStore()
const authStore = authUtilStore();
// 初始化购物车
onMounted(async () => {
  await cartStore.fetchCart()
})
// 获取当前订单信息
const currentOrder = computed(() => orderStore.currentOrder)
const showMessage = (type: any, message: any) => {// 显示消息提示
  ElMessage({
    type,
    message,
    duration: 1000,
  });
};
// 立即支付
const paySubmit = async () => {
  if (userInfo.status === '0') {
    ElMessage.warning('你已被冻结，无法支付');
    return;
  }
  try {
    if (orderStore.safeCurrentOrder.pay_type === 2) {
      const url: any = await alipay(orderStore.safeCurrentOrder.no, '购物');
      window.location.href = url;
    } else {
      await updatePayTime(orderStore.safeCurrentOrder.no);
      authStore.active = 3
      showMessage("success", "支付成功");
    }
  } catch (e) {
    console.log(e);
    showMessage("error", "支付失败");
  }
}
</script>

<template>
  <!-- main -->
  <div class="content-main">
    <!-- 订单信息 -->
    <div class="card">
      <div class="content-title">提交订单</div>
      <div class="order-content">
        <el-descriptions :column="1" border label-width="200" size="large">
          <el-descriptions-item label-align="center" label="订单编号" width="500">
            {{ currentOrder?.no }}
          </el-descriptions-item>
          <el-descriptions-item label-align="center" label="订单金额">
            ￥{{ currentOrder?.total.toFixed(2) }}
          </el-descriptions-item>
          <el-descriptions-item label-align="center" label="支付方式">
            {{ currentOrder?.pay_type === 1 ? '微信支付' : currentOrder?.pay_type === 2 ? '支付宝支付' : '银行卡支付' }}
          </el-descriptions-item>
        </el-descriptions>
        <button class="btn-primary" @click="paySubmit">立即支付</button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.btn-primary {
  background-color: #e81414;
  color: white;
  border: none;
  border-radius: 5px;
  padding: 10px 20px;
  font-size: 18px;
  cursor: pointer;
  margin-top: 40px;
}

.order-content {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 500px;
}

.content-title {
  padding: 5px 20px;
  font-size: 20px;
  background: #f2f2f2;
  border-bottom: 1px solid #ddd;
}

.card {
  border: 1px solid #ccc;
  width: 90%;
}

.content-main {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 0;
}
</style>